package com.neo.office.server.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neo.office.server.config.data.Result;
import com.neo.office.server.domain.entity.Department;
import com.neo.office.server.mapper.DepartmentMapper;
import com.neo.office.server.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 获取所有部门
     * @return
     */
    @Override
    public List<Department> getAllDepartment() {
        return departmentMapper.getAllDepartment(-1);
    }

    /**
     * 添加部门
     * @return
     */
    @Override
    public Result addDepartment(Department department) {
        department.setEnabled(true);
        departmentMapper.addDepartment(department);
        if (department.getResult() == 1){
            return Result.success("添加成功",department);
        }
        return Result.error("添加失败");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public Result deleteDepartment(Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteDepartment(department);
        if (department.getResult() == -2){
            return Result.error("该部门向还有子部门，删除失败");
        }
        if (department.getResult() == -1){
            return Result.error("该部门向还有员工，删除失败");
        }
        if (department.getResult() == 1){
            return Result.success("删除成功");
        }

        return Result.error("删除失败");
    }

}
